package com.stone.business.sbqxjl.action;

/**
 * <pre>
 * 设备缺陷记录
 * @author 
 * </pre>
 */

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.gzrwd.service.ZbjlCommonService;
import com.stone.business.sbqxjl.pojo.TSbqxjl;
import com.stone.utils.DateFormatTool;
import com.stone.utils.JsonUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

public class ddqxAction extends BaseAction {
	
	private static final long serialVersionUID = -4647594073681054487L;

	private ZbjlCommonService myService;
	
	private TSbqxjl entity;
	
	private String conditionid;

	@SuppressWarnings("unchecked")
	public String checkOut(){
		List ddqxList = null;
		String sql = "";
		Integer cid ;
		
		String[] ids = conditionid.split(",");
		
		sql = "select a.cid as id,a.serial_no as serialno,a.report_time as reporttime,a.reporter as reporter," +
				"a.content as content,a.removed as remove,a.category as category,get_bdzid(a.address) as bdzid,get_bdzname(a.address) as bdzname" +
				" from dm_defect a" +
				" where cid ";
				
		if(ids.length == 1){
			conditionid = ids[0];
			sql += " = " + conditionid;
		}else{
			conditionid=conditionid.substring(0, conditionid.length()-1);//by wangtao update20120326
			sql += " in (" + conditionid + ")"; 	
		}	
		
		ddqxList = 	myService.findListBySql(sql);
		
		if(ddqxList!=null&&ddqxList.size()>0){
			for(int i=0;i<ddqxList.size();i++){
				HashMap map = (HashMap) ddqxList.get(i);
				TSbqxjl sbqxjl = new TSbqxjl();
				sbqxjl.setQxbh((String) map.get("serialno"));
				sbqxjl.setBdz(map.get("bdzid").toString());
				sbqxjl.setBdzname((String) map.get("bdzname"));
			//	sbqxjl.setFxrq((Date) map.get("reporttime"));
				sbqxjl.setFxrq(DateFormatTool.formatToDate(
						(String) map.get("reporttime"), true));
				sbqxjl.setQxnr((String) map.get("content"));
				sbqxjl.setQxlb(((BigDecimal) map.get("category")).intValue()+"");
				//sbqxjl.setDefectid((Long)map.get("id"));
				sbqxjl.setDefectid(((BigDecimal) map.get("id")).longValue());
				sbqxjl.setState("1");
				myService.save(sbqxjl);
				
				cid =  ((BigDecimal) map.get("id")).intValue();
				
				myService.insertBySql("update dm_defect set flag = '1' where cid = '" + cid + "'");
			}

		}
		
		return "SUCCESS";
	}
	/**
	 * 弹出选择的checkbox tree
	 * 
	 * @return
	 * @throws AppException
	 */
	@SuppressWarnings("unchecked")
	public void getListExtTree() throws Exception {

		String bdzid=super.getRequest().getParameter("bdzid");
		
		String parentid = super.getRequest().getParameter("parentid");
		// 电压等级
		List<HashMap<String, Object>> list = null;
		
		if ("0".equals(parentid)) {
		
			list = myService.getListByDHql(
					" t.dydj || 'kv'  as id, dic.itemName as text, 0 as leaf,dic.sortid,'' as qtip", 
					"t.dydj = dic.itemId and dic.itemType = 'DYDJ' "+(bdzid!=null&&!bdzid.trim().equals("")?" and t.id='"+bdzid.trim()+"'":""), 
					null, 
					" group by t.dydj,  dic.itemName,dic.sortid order by dic.sortid", 
					" TBdz t, TDictionary dic ");
			// 变电站
		} else if (parentid.endsWith("kv")) {
			
			list = myService.getListByDHql(
					" t.id||'bdz' as id, t.bdzname as text, 0 as leaf,'' as qtip", 
					" t.dydj = ? "+(bdzid!=null&&!bdzid.trim().equals("")?" and t.id='"+bdzid.trim()+"'":""), 
					new Object[] {parentid.replace("kv", "")}, 
					" order by t.sortid", 
					" TBdz t");
			// 间隔
		} else if(parentid.endsWith("bdz")){
			System.out.println("树间隔电压:"+parentid);
			/*list = myService.getListByDHql(
					" distinct t.bdzid||'@'||t.dydj||'jgdy' as id, t.dydj||'KV' as text, 0 as leaf,'' as qtip", 
					" t.bdzid = ? ", 
					new Object[] {Long.parseLong(parentid.replace("bdz", ""))}, 
					" ", 
					" TBdzJg t");*/
			list = myService.getListByDHql(
					" t.bdzid||'@'||t.dydj||'jgdy' as id, dic.itemName as text, 0 as leaf, '' as qtip",  
					"t.dydj = dic.itemId and t.state ='1' and dic.itemType = 'DYDJ' and t.bdzid= ? ", 
					new Object[] {Long.parseLong(parentid.replace("bdz", ""))}, 
					" group by t.dydj,t.bdzid,  dic.itemName,dic.sortid order by dic.sortid", 
					"TBdzJg t, TDictionary dic ");	
			
		}else if(parentid.endsWith("jgdy")){
			parentid=parentid.replace("jgdy", "");
			String[] array=parentid.split("@");
			String sql="select t.id||'sb!' as id, t.jgmc as text,case when getCount(t.id)>0 then 0 else 1 end as leaf,'间' as qtip "+
			"from SYS_BDZ_JG t"+
			" where t.state = '1' and t.bdzid = ? and t.dydj=? "+
			" order by t.id ";
			/*list = myService.getListByDHql(
					" t.id||'sb!' as id, t.jgmc as text, 0 as leaf,'间隔' as qtip", 
					" t.state = '1' and t.bdzid = ? and t.dydj=?", 
					new Object[] {Long.parseLong(array[0]),array[1].trim()}, 
					" order by t.id", 
					" TBdzJg t");*/
			list=myService.findListBySql(sql, new Object[] {Long.parseLong(array[0]),array[1].trim()});
		}else if(parentid.endsWith("sb!")){
			parentid=parentid.replace("sb!", "");
			String sql="select to_char(t.id) as id,t.jgmc as text,case when (select count(1) from tab_equipment_base e where e.typeid=t.id)>0 then 0 else 1 end as leaf,'' as qtip "+ 
			" from SYS_BDZ_JG t where t.state='2' and parentid="+Long.parseLong(parentid)+
			" union all "+
			"select t.id||'@'||d.item_id as id,s.jgmc||t.yxbh||d.item_name as text,1 as leaf,'s' as qtip "+
			"from tab_equipment_base t,SYS_BDZ_JG s,sys_dictionary d "+
			" where t.typeid=s.id and s.state='1' and t.sblx=d.item_id and d.item_type='SBLX' and t.typeid="+Long.parseLong(parentid);
			list=myService.findListBySql(sql);
		}else{
			parentid=parentid.trim();
			String sql="select t.id||'@'||d.item_id as id,getsbname('"+parentid+"')||t.yxbh||d.item_name as text,1 as leaf,'s' as qtip from tab_equipment_base t,sys_dictionary d where t.sblx=d.item_id and d.item_type='SBLX' and t.state='2' and t.typeid="+Long.parseLong(parentid);
			list=myService.findListBySql(sql);
			System.out.println(sql);
		}

        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(list, jsonConfig);

        super.getResponse().getWriter().print(json.toString());
		
	}
////////////////////by wangtao add20130418
	@SuppressWarnings("unchecked")
	public String saveFlag(){
		List ddqxList = null;
		String sql = "";
		Integer cid ;
		
		String[] ids = conditionid.split(",");
		
		sql = " ";
				
		if(ids.length == 1){
			conditionid = ids[0];
			sql += " = " + conditionid;
		}else{
			conditionid=conditionid.substring(0, conditionid.length()-1);//by wangtao update20120326
			sql += " in (" + conditionid + ")"; 	
		}	
		
		myService.insertBySql("update dm_defect set flag = '1' where cid " + sql + "");
				
		return "SUCCESS";
	}
	////////////////////////////////////////////////
	public ZbjlCommonService getMyService() {
		return myService;
	}

	public void setMyService(ZbjlCommonService myService) {
		this.myService = myService;
	}

	public TSbqxjl getEntity() {
		return entity;
	}

	public void setEntity(TSbqxjl entity) {
		this.entity = entity;
	}


	public String getConditionid() {
		return conditionid;
	}


	public void setConditionid(String conditionid) {
		this.conditionid = conditionid;
	}




}
